const Koa = require('koa')
const KoaRouter = require('koa-router')
const cors = require('koa-cors')

const app = new Koa()
const router = new KoaRouter()

app.use(cors()).use(router.routes()).use(router.allowedMethods())



router.get('/',(ctx,next)=>{
	ctx.body = '首页'
	console.log('请求体',ctx.request)
	console.log('响应体',ctx.response)
	// console.log('响应体')
	// console.log('响应体')
})
router.get('/test',(ctx,next)=>{
	ctx.body = {
		code:200,
		data:{
			arr:['a','b','c'],
			arr2:[1,2,3]
		}
	}
	// console.log('响应体')
	// console.log('响应体')
})
router.get('/test2',(ctx,next)=>{
	ctx.body = {
		code:200,
		data:{
			arr:['a','b','c'],
			arr2:[1,2,3],
			arr3:{
				age:11,
				name:'bian'
			}
		}
	}
	// console.log('响应体')
	// console.log('响应体')
})
const initData = require('./datas/index.json')
router.get('/getInitData',(ctx,next)=>{
	ctx.body = {
		code:200,
		data:initData
	}
	
	// console.log('请求体',ctx.request.query,ctx.request.params,ctx.request.body)
	// console.log('响应体',ctx.response.query,ctx.response.body)
})

app.listen(8008,error=>{
	if(error){
		console.log('服务器启动失败')
		console.log(error)
	}else{
		console.log('服务器启动成功')
		console.log('入口地址为 http://localhost:8008')
	}
})